/*
 * File Name:               XBTreeDebuger.cs
 * 
 * Description:             普通类
 * Author:                  lisiyu <576603306@qq.com>

 * Create Date:             2017/08/07
 */

#if XBTREE_DEBUG
using UnityEngine;
using UnityEditor;
using XBehaviorTree;
using BehaviorDesigner.Runtime.Tasks;
using BehaviorDesigner.Editor;

public class XBTreeDebuger : EditorWindow
{
    [MenuItem("Tools/Compile/简易AI调试/打开窗口")]
    static void onOpen()
    {
        GetWindow<XBTreeDebuger>("简易AI调试器").Show(true);
    }

    private int[] mIds;
    private string[] mIdNames;
    private int mSelectId;
    private Vector2 mScrollVec;
    private void OnGUI()
    {
        if (GameMatch.Singleton == null)
            return;

        var match = GameMatch.Singleton;
        if(mIds == null || mIds.Length == 0)
        {
            mIds = new int[match.playerList.Length];
            mIdNames = new string[match.playerList.Length];
            for (int i = 0; i < mIds.Length; i++)
            {
                var tmpPlayer = match.playerList[i];
                mIds[i] = tmpPlayer.ID;
                mIdNames[i] = mIds[i] + " --- " + tmpPlayer.name + " --- " + tmpPlayer.currentLocation;
            }
            mSelectId = mIds[0];
        }

        mSelectId = EditorGUILayout.IntPopup("角色ID", mSelectId, mIdNames, mIds);
        var aiTree = getAITree(match, mSelectId);

        offset = new GUIStyle() { margin = new RectOffset(16, 0, 0, 0) };
        success = new GUIStyle(EditorStyles.whiteBoldLabel);
        success.alignment = TextAnchor.MiddleLeft;
        success.normal.background = getColorTexture(GreenColor);
        success.normal.textColor = Color.white;
        failure = new GUIStyle(EditorStyles.whiteBoldLabel);
        failure.alignment = TextAnchor.MiddleLeft;
        failure.normal.background = getColorTexture(RedColor);
        failure.normal.textColor = Color.white;
        inactive = new GUIStyle(EditorStyles.whiteBoldLabel);
        inactive.alignment = TextAnchor.MiddleLeft ;
        inactive.normal.background = getColorTexture(BlackColor);
        inactive.normal.textColor = Color.white;

        EditorGUILayout.BeginVertical();
        mScrollVec = EditorGUILayout.BeginScrollView(mScrollVec);
        draw(aiTree.mTask);
        EditorGUILayout.EndScrollView();
        EditorGUILayout.EndVertical();
    }

    private XBehaviorTree.XBehaviorTree getAITree(GameMatch match, int playerId)
    {
        var player = match.GetPlayer(playerId);
        if(player.team.GetControllPlayer() == player)
        {
            return player.team.aiTree;
        }
        else
        {
            return player.aiTreeLite;
        }
    }

    private GUIStyle offset;
    private GUIStyle success;
    private GUIStyle failure;
    private GUIStyle inactive;

    private GUIStyle getGUIStyle(TaskStatus taskStatus)
    {
        if (taskStatus == TaskStatus.Success)
            return success;
        else if (taskStatus == TaskStatus.Failure)
            return failure;
        else
            return inactive;
    }

    private void draw(ITask_BTree task)
    {
        var style = getGUIStyle((task as ITask_Debug).DebugStatus);
        EditorGUILayout.BeginHorizontal(style);
        if(GUILayout.Button(task.GetType().Name, style))
        {
            if(task is ITask_Debug)
            {
                (task as ITask_Debug).ViewChild = !(task as ITask_Debug).ViewChild;
            }
        }
        EditorGUILayout.LabelField((task as ITask_Debug).FriendlyName, style);
        EditorGUILayout.EndHorizontal();

        if(task is ITask_Debug)
        {
            if (!(task as ITask_Debug).ViewChild)
                return;
        }

        if (task is Sequence_BTree)
        {
            EditorGUILayout.BeginVertical(offset);
            var childs = (task as Sequence_BTree).mChilds;
            foreach (var child in childs)
            {
                draw(child);
            }
            EditorGUILayout.EndVertical();
        }
        else if (task is Selector_BTree)
        {
            EditorGUILayout.BeginVertical(offset);
            var childs = (task as Selector_BTree).mChilds;
            foreach (var child in childs)
            {
                draw(child);
            }
            EditorGUILayout.EndVertical();
        }
        else if (task is Inverter_BTree)
        {
            EditorGUILayout.BeginVertical(offset);
            var child = (task as Inverter_BTree).mChild;
            draw(child);
            EditorGUILayout.EndVertical();
        }
        else
        {
            task.display("", autoCreate: false, displayFileds: true, displayProperties: false, editMode: false, onlyValueType: true);
        }
    }

    private readonly static Color GreenColor = new Color32(38, 158, 111, 255);
    private readonly static Color RedColor = Color.red;
    private readonly static Color BlackColor = Color.black;
    private static Texture2D getColorTexture(Color c)
    {
        Texture2D tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
        tex.SetPixel(0, 0, c);
        tex.Apply();
        return tex;
    }
}
 #endif